package dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import entities.OrdenDeCompraBean;
import hbt.HibernateUtil;

public class OrdenDeCompraDAO {
	
	public Double totalOrdenCompra(dto.OrdenDeCompra orden)
	{
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.openSession();
		s.beginTransaction();
		
		
	
		Double total = (Double) s.createQuery("select sum(i.cantidad*p.precio) as total "
				+ " from OrdenDeCompraBean o "
				+ "inner join o.items i "
				+ "inner join i.material m "
				+ "inner join m.precios p "
				+ "where o.numero = :numero").setParameter("numero", orden.getNumero()).uniqueResult();
		
		s.getTransaction().commit();
		s.close();
		return total;
		
	}
	
	public void grabar(OrdenDeCompraBean orden)
	{
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.openSession();
		s.beginTransaction();
		s.saveOrUpdate(orden);
		s.getTransaction().commit();
		s.close();
	}

}
